﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MediaWiki.Office.Model.Utilities
{
  public static class MiscellaneousUtils
  {
    //public static SmartEnumerable<T> ToSmart<T>(this IEnumerable<T> enumerable)
    //{
    //  return new SmartEnumerable<T>(enumerable);
    //}

    public static T EvaluateUntilValue<T>(IEnumerable<Func<T>> valueGetters)
    {
      foreach (Func<T> valueGetter in valueGetters)
      {
        T value = valueGetter();
        if (value != null)
          return value;
      }

      return default(T);
    }

    public static List<object> AppendValues<T>(IEnumerable<Func<T>> valueGetters)
    {
      List<object> values = new List<object>();

      foreach (Func<T> valueGetter in valueGetters)
      {
        T value = valueGetter();

        if (value != null)
        {
          IEnumerable enumerable = value as IEnumerable;
          if (enumerable == null)
            throw new Exception("Value does not implement IEnumerable.");

          values.AddRange(enumerable.Cast<object>());
        }
      }

      return values;
    }

    public static bool Equals<T>(T objA, T objB)
    {
      IEquatable<T> equatableA = objA as IEquatable<T>;
      IEquatable<T> equatableB = objB as IEquatable<T>;

      if (equatableA != null || equatableB != null)
      {
        if (equatableA == null && equatableB == null)
          return true;

        if ((equatableA != null && equatableB != null) && equatableA.Equals(equatableB))
          return true;

        return false;
      }
      else
      {
        return object.Equals(objA, objB);
      }
    }

    public static string ToString(object value)
    {
      return (value != null) ? value.ToString() : "{null}";
    }
  }
}